<?php
class AppController extends Controller {

	var $components = array('Authake.Authake', 'Session', 'RequestHandler','Email'); 
	var $helpers = array('Html', 'Javascript', 'Session', 'GershImage', 'Lightbox', 'Ajax', 'Text', 'Time', 'Number', 'Textile','Cache','Authake.Authak3');
	
	
	function beforeRender() {
		/*if($this->RequestHandler->isAjax() || $this->RequestHandler->isXml()) { 
			Configure::write('debug', 0); 
		} */
	}
	
	function sendEmail($subject, $view, $to=null, $from_email = null, $from = null) {
		$from = Configure::read('Newsletter.from');
		$from_email = Configure::read('Newsletter.from_email');
		if(is_array($to)) {
			foreach ($to as $key => $value) {
				$this->Email->reset();
				$this->Email->from    = $from . ' <'.$from_email.'>';
				$this->Email->subject = $subject;
				$this->Email->template = $view;
				$this->Email->sendAs = 'both';
				$this->set('name', $value);
				$this->Email->to = $key;
				$this->Email->send();
			}
		} else {
			$this->Email->from    = $from . ' <'.$from_email.'>';
			$this->Email->subject = $subject;
			$this->Email->template = $view;
			$this->Email->sendAs = 'both';
			$this->Email->to = $to; //gerhard@localhost
			$this->Email->send();
		}
		
	}
	
	function beforeFilter() {
		parent::beforeFilter(); 
		Configure::write('Config.language', 'no');
		$admingroup = Configure::read('Authake.adminGroup');
		//
		$is_admin = false;
		if ($this->Authake->isLogged()) {
			
			$user = array('User' => array(
				'id' => $this->Session->read('Authake.id'),
				'username' => $this->Session->read('Authake.login'),
				'url' => '/authake/user/index',
				'logout' => '/authake/user/logout',
				'email' => $this->Session->read('Authake.email'),
				'is_admin' => false
				));
			if ($this->Authake->isMemberOf($admingroup)) {
				$user['User']['is_admin'] = true;
				$this->set('is_admin', true);
				$is_admin = true;
			}
			$this->set('auth_user', $user);
			
			//$this->Session->read
		} 
		
		/* SETTINGS */
		Configure::load('settings');
		
		$this->Authake->beforeFilter($this);
		if(Configure::read('debug') != 0 or $is_admin) {
			Configure::write('Site.footer_js', ""); 
			Configure::write('Site.google_ad_bottom', ""); 
			Configure::write('Site.google_ad_sidebar', ""); 
		}
		//debug($this->Authake->isMemberOf());
        return true;
	}
	
	function is_admin() {
		$admingroup = Configure::read('Authake.adminGroup');
		if($this->Authake->isMemberOf($admingroup)) 
			return true;
		else 
			return false;
	}

}
?>